package com.ecity.table 
{
	import com.ecity.events.GameEvent;
	import com.ecity.util.HashMap;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class TouchHands extends Sprite {
		private var _hl_01:Bitmap;
		private var _hl_02:Bitmap;
		private var _hl_03:Bitmap;
		private var _hl_04:Bitmap;
		
		private var _hr_01:Bitmap;
		private var _hr_02:Bitmap;
		private var _hr_03:Bitmap;
		private var _hr_04:Bitmap;
		
		private var _hitable:Boolean = false;
		private var _inited:Boolean = false;
		
		private static const POSI_L:Array = [[300, 560, 0], [800, 270, -90], [480, 0, 180], [0, 70, 90]];
		private static const POSI_R:Array = [[480, 560, 0], [800, 70, -90], [300, 0, 180], [0, 270, 90]];
		
		private var hitmap:HashMap = new HashMap();
		
		public function TouchHands(){

		}
		
		public function hitTest(rx:int, ry:int):void {
			if(_hitable){
				for (var i:int = 0; i < 4; i++) {
					var lh:Bitmap = this["_hl_0" + (i + 1)] as Bitmap;
					var rh:Bitmap = this["_hr_0" + (i + 1)] as Bitmap;
					var rect:Rectangle = new Rectangle(rx * 20 - lh.x - this.x, ry * 20 - this.y - lh.y, 20, 20);
					var rect2:Rectangle = new Rectangle(rx * 20 - rh.x - this.x, ry * 20 - this.y - rh.y, 20, 20);
					var isHitL:Boolean = lh.bitmapData.hitTest(new Point(), 0x99, rect);
					var isHitR:Boolean = rh.bitmapData.hitTest(new Point(), 0x99, rect2);
					if (isHitL) {
						check("L", i);
					}
					if (isHitR) {
						check("R", i);
					}
				}
			}
		}
		
		private function check(lr:String,index:int):void {
			hitmap.put(lr + index, 0);
			if (hitmap.size() > 1) {
				var oppo:String;
				if (lr == "L") {
					oppo = "R";
				}else {
					oppo = "L";
				}
				if (hitmap.containsKey(oppo + index)) {
					this.dispatchEvent(new GameEvent(GameEvent.REQ_UNCOVER, {index:index} ));
				}
			}
			var stid:uint = setTimeout(removeHitPot,1000,lr + index,stid);
		}
		
		private function removeHitPot():void {
			clearTimeout(arguments[1]);
			hitmap.remove(arguments[0]);
		}
		
		public function hide():void {
			for (var i:int = 0; i < 4; i++) {
				this["_hl_0" + (i + 1)].alpha = 1;
				this["_hr_0" + (i + 1)].alpha = 1;
			}
			_hitable = false;
		}
		
		private function initHands():void {
			for (var i:int = 0; i < 4; i++) {
				this["_hl_0" + (i + 1)] = UIFactory.getBitmapbyName("hand_left_"+ (i + 1));
				this["_hr_0" + (i + 1)] = UIFactory.getBitmapbyName("hand_right_" + (i + 1));
				this["_hl_0" + (i + 1)].x = POSI_L[i][0];
				this["_hl_0" + (i + 1)].y = POSI_L[i][1];
				this["_hr_0" + (i + 1)].x = POSI_R[i][0];
				this["_hr_0" + (i + 1)].y = POSI_R[i][1];
				this.addChild(this["_hl_0" + (i + 1)]);
				this.addChild(this["_hr_0" + (i + 1)]);
				_inited = true;
			}
		}
		
		public function show():void {
			if (_inited) {
				for (var i:int = 0; i < 4; i++) {
					this["_hl_0" + (i + 1)].alpha = 1;
					this["_hr_0" + (i + 1)].alpha = 1;
				}
				_hitable = true;
			}else {
				initHands();
				show();
			}
		}
		
		public function flicker():void {
			
		}
		
	}

}